<?php
class NhomhpsController extends AppController {

	var $name = 'Nhomhps';
	var $uses = array('Nhomhp','Monthamgianhom','Mon');
	
	function index() {
		$this->Nhomhp->recursive = 0;
		$this->set('nhomhps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid nhomhp', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('nhomhp', $this->Nhomhp->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Nhomhp->create();
			if ($this->Nhomhp->save($this->data)) {
				$this->Session->setFlash(__('The nhomhp has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The nhomhp could not be saved. Please, try again.', true));
			}
		}
		$nhomhps = $this->Nhomhp->Nhomhp->find('list');
		$ctdts = $this->Nhomhp->Ctdt->find('list');
		$this->set(compact('nhomhps', 'ctdts'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid nhomhp', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Nhomhp->save($this->data)) {
				$this->Session->setFlash(__('The nhomhp has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The nhomhp could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Nhomhp->read(null, $id);
		}
		$nhomhps = $this->Nhomhp->Nhomhp->find('list');
		$ctdts = $this->Nhomhp->Ctdt->find('list');
		$this->set(compact('nhomhps', 'ctdts'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for nhomhp', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Nhomhp->delete($id)) {
			$this->Session->setFlash(__('Nhomhp deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Nhomhp was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function xemnhomhp($idctdt = null,$idnhom=null)
	{
		$nhom = $this->Nhomhp->find('all',array('conditions'=>array('Nhomhp.ctdt_id'=>$idctdt),'fields'=>array('id','tennhom','nhomhp_id')));
		$data[] = array("id"=>-1,"tennhom"=>'Chương trình đào tạo',"nhomhp_id"=>0);
		//debug($data);
		foreach ($nhom as $row)
		{
			if (is_null($row['Nhomhp']['nhomhp_id']))
				$data[] = array("id"=>$row['Nhomhp']['id'],"tennhom"=>$row['Nhomhp']['tennhom'],"nhomhp_id"=>-1);
				
			else
				$data[] = array("id"=>$row['Nhomhp']['id'],"tennhom"=>$row['Nhomhp']['tennhom'],"nhomhp_id"=>$row['Nhomhp']['nhomhp_id']);
		}
//debug($data);
		if(!empty($idnhom))
		{
			$this->data = $this->Nhomhp->read(null,$idnhom);
		}
		
		$nhom =array();
		$this->data = $this->Nhomhp->read(null,$idnhom);
		$nhomcon = $this->Nhomhp->find('list',array('conditions'=>array('nhomhp_id'=>$idnhom)));
		if(empty($nhomcon))
		{
			$monthamgianhoms = $this->Monthamgianhom->find('all',array('conditions'=>array('Monthamgianhom.nhomhp_id'=>$idnhom),'recursive'=>0));
			foreach ($monthamgianhoms as $monthamgianhom)
			{
				$nhom[$monthamgianhom['Monthamgianhom']['mon_id']]=$monthamgianhom['Mon']['mamom'].'-'.$monthamgianhom['Mon']['tenMon'];
			}
		}
		$nhcon = $this->Nhomhp->find('list',array('conditions'=>array('ctdt_id'=>$idctdt)));
		$mctdts = $this->Monthamgianhom->find('all',array('recursive'=>1,'conditions'=>array('Monthamgianhom.nhomhp_id'=>$nhcon)));
		foreach ($mctdts as $mctdt)
		{
				$monctdts[$mctdt['Monthamgianhom']['mon_id']]=$mctdt['Mon']['mon'];
		}
		$mons = $this->Mon->find('list',array('conditions'=>array('NOT'=>array('id'=>array_keys($nhom))),'fields'=>array('mon')));
		
		$this->set(compact('data','idnhom','idctdt','nhom','mons','monthamgianhoms','monctdts'));
	}

	function capnhatnhom($idctdt=null,$idnhom = null)
	{
		$this->layout = 'ajax';
		$nhom =array();
		$this->data = $this->Nhomhp->read(null,$idnhom);
		$nhomcon = $this->Nhomhp->find('list',array('conditions'=>array('nhomhp_id'=>$idnhom)));
		if(empty($nhomcon))
		{
			$monthamgianhoms = $this->Monthamgianhom->find('all',array('conditions'=>array('Monthamgianhom.nhomhp_id'=>$idnhom),'recursive'=>0));
			foreach ($monthamgianhoms as $monthamgianhom)
			{
				$nhom[$monthamgianhom['Monthamgianhom']['mon_id']]=$monthamgianhom['Mon']['mamom'].'-'.$monthamgianhom['Mon']['tenMon'];
			}
		}
		$nhcon = $this->Nhomhp->find('list',array('conditions'=>array('ctdt_id'=>$idctdt)));
		$mctdts = $this->Monthamgianhom->find('all',array('recursive'=>1,'conditions'=>array('Monthamgianhom.nhomhp_id'=>$nhcon)));
		foreach ($mctdts as $mctdt)
		{
				$monctdts[$mctdt['Monthamgianhom']['mon_id']]=$mctdt['Mon']['mon'];
		}
		$mons = $this->Mon->find('list',array('conditions'=>array('NOT'=>array('id'=>array_keys($nhom))),'fields'=>array('mon')));
		$this->set(compact('idnhom','idctdt','nhom','mons','monthamgianhoms','monctdts'));
	}

	function luunhomhp($idctdt = null,$idnhom=null)
	{
			if (!empty($this->data)) {
			if ($this->Nhomhp->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu.', true));
				$this->redirect(array('action' => 'xemnhomhp',$idctdt,$idnhom));
			} else {
				$this->Session->setFlash(__('Không thể lưu vui lòng thử lại', true));
			}
			$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
		}
	}

	function luumonnhom($idctdt = null,$idnhom=null)
	{
		if (!empty($this->data))
		{
			if(isset($this->data['Nhomhp']['dsmonthem']))
			{
				foreach ($this->data['Nhomhp']['dsmonthem'] as $key =>$value)
				{
					if(!isset($this->data['Nhomhp']['dsco'][$key]))
					{
						$record['Monthamgianhom']['mon_id'] = $key;
						$record['Monthamgianhom']['nhomhp_id'] = $idnhom;
						//$record['Monthamgianhom']['nhomhp_id']
						$this->Monthamgianhom->saveAll($record);
					}
				}
			}
			if(isset($this->data['Nhomhp']['dsmonxoa']))
			{
				foreach ($this->data['Nhomhp']['dsmonxoa'] as $key =>$value)
				{
					if(isset($this->data['Nhomhp']['dsco'][$key]))
					{
						$record['Monthamgianhom']['mon_id'] = $key;
						$record['Monthamgianhom']['nhomhp_id'] = $idnhom;
						$this->Monthamgianhom->deleteAll(array('Monthamgianhom.mon_id'=>$key,'Monthamgianhom.nhomhp_id'=>$idnhom));
					}
				}
			}
			$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
		}
	}

	function themnhom($idctdt = null,$idnhom=null)
	{
		if(!empty($this->data))
		{
			$this->data['Nhomhp']['nhomhp_id'] = ($idnhom ==-1?null:$idnhom);
			$this->data['Nhomhp']['ctdt_id'] = $idctdt;
			if($this->Nhomhp->save($this->data))
			{
				$this->Session->setFlash('Đã lưu');
				$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
			}
			else
			{
				$this->Session->setFlash('Không thể lưu vui lòng thử lại.');
			}
		}
		$this->set(compact('idctdt','idnhom'));
	}

	function xoanhom($idctdt = null,$idnhom=null)
	{
		if (!$idnhom) {
			$this->Session->setFlash(__('Nhóm không tồn tại', true));
			$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
		}
		if ($this->Nhomhp->delete($idnhom)) {
			$this->Session->setFlash(__('Đã Xóa', true));
			$this->redirect(array('action'=>'xemnhomhp',$idctdt));
		}
		$this->Session->setFlash(__('Không thể xóa', true));
		$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
	} 

	function luuchitietmon($idctdt = null,$idnhom=null)
	{
		
		$mons = array_keys($this->data['Nhomhp']['batbuoc']);
		foreach ($mons as $value)
		{
			$this->Monthamgianhom->updateAll(array('batbuoc'=>$this->data['Nhomhp']['batbuoc'][$value],'monthamgianhom_id'=>empty($this->data['Nhomhp']['tienquyet'][$value])? null: $this->data['Nhomhp']['tienquyet'][$value]),array('Monthamgianhom.id'=>$value)); 
		}
		$this->Session->setFlash('Đã lưu');
		$this->redirect(array('action'=>'xemnhomhp',$idctdt,$idnhom));
	}
}
